package com.yxf.trytocatchoutofmemory;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Binder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private ArrayList<byte[]> mArrayList = new ArrayList<byte[]>();

    private Button mOutButton, mInButton, mAddButton, mActionButton, mGcButton;
    private TextView mLeftMemoryView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mOutButton = findViewById(R.id.out);
        mInButton = findViewById(R.id.in);
        mAddButton = findViewById(R.id.add);
        mActionButton = findViewById(R.id.action);
        mGcButton = findViewById(R.id.gc);
        mLeftMemoryView = findViewById(R.id.left);
        mOutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                byte[] values = null;
                try {
                    values = new byte[1024 * 1024 * 1024];
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                }
                if (values == null) {
                    Log.d(TAG, "onClick: values is null");
                } else {
                    Log.d(TAG, "onClick: values not null");
                }
                updateLeftMemoryView();
            }
        });
        mInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    byte[] values = new byte[1024 * 1024 * 1024];
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                }
                updateLeftMemoryView();
            }
        });
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mArrayList.add(new byte[1024 * 1024 * 10]);
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                }
                updateLeftMemoryView();
            }
        });
        mActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                byte[] bytes = new byte[1024 * 1024 * 10];
                updateLeftMemoryView();
            }
        });
        mGcButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.gc();
                updateLeftMemoryView();
            }
        });
        updateLeftMemoryView();

    }

    private void updateLeftMemoryView() {
        String s = String.format("内存总计 : %#.3f , 剩余 : %#.3f", (Runtime.getRuntime().totalMemory() * 1.0 / 1024 / 1024), (Runtime.getRuntime().freeMemory() * 1.0 / 1024 / 1024));
        mLeftMemoryView.setText(s);
    }
}
